package com.qfedu.controller;

import com.qfedu.entity.CompanyEntity;
import com.qfedu.kit.StringUtil;
import com.qfedu.util.uuidutil;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;

@Controller
public class CompanyController {

    @RequestMapping("/jumpCompany")
    public String jumpCompany() {
        return "BusinessInformation";
    }


   /* @RequestMapping("/upload")
    @ResponseBody
    public String upload(HttpServletRequest request, CompanyEntity ce, @RequestParam(name = "file")MultipartFile omstring) {

        String filename = uuidutil.getuuid() + omstring.getOriginalFilename();

        try {
            InputStream in = omstring.getInputStream();

            FileOutputStream fos = new FileOutputStream("47.99.61.196\\images\\");
            InputStream is = omstring.getInputStream();
            FileCopyUtils.copy(is, fos);//将前台上传过来的文件,保存到服务器上面

        } catch (Exception e) {
            e.printStackTrace();
        }

        String aimagename = filename;

        System.out.println("----ii:__"+aimagename);

        return "";
    }*/


    @RequestMapping("/upload")
    @ResponseBody
    public void uploadFile(@RequestParam(name = "file") MultipartFile multipartFile) {

        //指定文件上传存放的路径
        String fileDir = "C:\\Users\\Administrator\\Desktop\\img";
        File dir = new File(fileDir);

        //判断目录存不存在，不存在创建目录
        if (!dir.exists()){
            dir.mkdirs();
        }

        //生成新的文件名，防止文件名重复而导致文件覆盖，
        String originalFilename = multipartFile.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf('.'));

        //生成新的文件名
        String newFileName = uuidutil.getuuid() + suffix;

        //放入文件夹
        File file = new File(dir, newFileName);

        //开始上传
        try {
            multipartFile.transferTo(file);
            System.out.println("上传文件成功！");
        } catch (IOException e) {
            System.out.println("上传文件失败！");
            e.printStackTrace();
        }

        if (uploadToFtp(file)){
            System.out.println("上传至ftp服务器！");
        }else {
            System.out.println("上传至ftp服务器失败!");
        }

        //上传至服务器


    }

        public boolean uploadToFtp(File file){
            FTPClient ftpClient = new FTPClient();

            try {
                //连接服务器
                System.out.println("1");
                ftpClient.connect("49-235-48-146");
                System.out.println("2");
                //进行登陆
                ftpClient.login("root","Fy1224241826");
                System.out.println("3");

                //改变工作目录（按自己需要是否改变）
                //只能选择local_root下已存在的目录

                ftpClient.changeWorkingDirectory("images");
                System.out.println("4");
                //设置文件类型为二进制文件
                ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

                ftpClient.enterLocalPassiveMode();


                ftpClient.storeFile(file.getName(),new FileInputStream(file));
                System.out.println(file.getName());


            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }

}







